#include "Channel.h"
#include "EventLoop.h"

#include <poll.h>

const int Channel::kNoneEvent = 0;
const int Channel::kReadEvent = POLLIN | POLLPRI;
// POLLIN 普通或优先级带数据可读  POLLPRI 高优先级数据可读
const int Channel::kWriteEvent = POLLOUT;
// POLLOUT 普通数据可写

Channel::Channel(EventLoop *loop, int fd) : loop_(loop), fd_(fd), events_(0), revents_(0), index_(-1) {}

void Channel::update() {
    loop_->updateChannel(this);
}


void Channel::handleEvent() {
    if(revents_ & (POLLERR | POLLNVAL)){
        if(errorCallback_) errorCallback_();
        // 出现错误调用错误回调函数
    }
    if(revents_ & (POLLIN | POLLPRI | POLLRDHUP)){
        if(readCallback_) readCallback_();
    }
    if(revents_ & (POLLOUT)){
        if(writeCallback_) writeCallback_();
    }
}